/**
 * Created by Administrator on 2017/1/17.
 */
var SCENIC;
var SHOP;
var reservearray=[];
var shopArray = [];
var LOGIN;
var CHOOSE, SPOT;
var CART, COMFIRMORDER;
var MINEINFO, MINEPRO, MINEORDER, MINEOPINION, ADMINPEND, MINEEXPEND, PENDORDER;
var user = {id: "abc"};

define(function (require, exports, module) {
    'use strict';
    Date.prototype.Format = function (fmt) { //author: meizz
            var o = {
                "M+": this.getMonth() + 1,                 //月份
                "d+": this.getDate(),                    //日
                "h+": this.getHours(),                   //小时
                "m+": this.getMinutes(),                 //分
                "s+": this.getSeconds(),                 //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds()             //毫秒
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }

    var initApp = function () {
        // //初始化签证对象
        // $.get( '/data/getTicket?url='+ encodeURIComponent(window.location.href.split('#')[0]), function(result){
        //     if(!result.success) toast.show(result.msg);
        //     var sign = result.sign;
        //     wx.config({
        //         debug: false,
        //         appId: sign.appid,
        //         timestamp: sign.timestamp,
        //         nonceStr: sign.noncestr,
        //         signature: sign.signature,
        //         jsApiList: [
        //             'previewImage',
        //             'chooseImage',
        //             'uploadImage',
        //             'chooseWXPay',
        //             'translateVoice',
        //             'onMenuShareTimeline',
        //             'onMenuShareAppMessage',
        //             'onMenuShareQQ',
        //             'onMenuShareWeibo',
        //             'onMenuShareQZone',
        //             'getLocation',
        //             'startRecord',
        //             'stopRecord',
        //             'translateVoice'
        //         ]
        //     });
        // });
        $.ajax({
            type: "GET",
            url: "/user/usermessage?userid="+user.id,
            dataType: "json",
            success: function (res) {
                if(res.data.length>0){
                    $("#homepage").attr('src', "images/bar/homepagel.png");
                    $("#shopcart").attr('src', "images/bar/shopcart.png");
                    $("#mine").attr('src', "images/bar/barcenter.png");
                    $("#shouye").addClass("on-mine");
                    $("#gouwuche").removeClass("on-mine");
                    $("#wode").removeClass("on-mine");
                    spotView();
                }else {
                    seajs.use('/script/login', function (login) {
                        $('#scenicbody').html(new EJS({url: "views/login.ejs"}).render({

                        }));
                        LOGIN=login;
                    })
                }
            }
        })
    }
    function spotView() {
        $.get('/scenicspot/sceniclist', function (res) {
            seajs.use("/script/scenicspot/sceniclist", function (scenic) {
                $("#scenicbody").html(new EJS({url: "/views/scenicspot/sceniclist.ejs"}).render({
                    datas: res.datas
                }));
                SCENIC = scenic;
            });
        })
    }

    function shoplist(sid) {
        $.get('/scenicspot/shoplist?sid=' + sid, function (res) {
            seajs.use("/script/scenicspot/shoplist", function (shop) {
                $("#scenicbody").html(new EJS({url: "/views/scenicspot/shoplist.ejs"}).render({
                    datas: res.datas
                }));
                SHOP = shop;
                SHOP.adddel();
                SHOP.timechoose();
            });
        })
    }

    //购物车
    function shoppingCart() {
        $("#homepage").attr('src', "images/bar/homepage.png");
        $("#shopcart").attr('src', "images/bar/shopcartl.png");
        $("#mine").attr('src', "images/bar/barcenter.png");
        $("#shouye").removeClass("on-mine");
        $("#gouwuche").addClass("on-mine");
        $("#wode").removeClass("on-mine");
        $.get('/cart/shoppingcart?userid=' + user.id, function (res) {
            shopArray=res.data;
                seajs.use("/script/shoppingcart/shoppingcart", function (cart) {
                    $("#scenicbody").html(new EJS({url: "/views/shoppingcart/shoppingcart.ejs"}).render({
                        data: res.data
                    }));
                    CART = cart;
                });
        })
    }


    //确认订单
    function comfirmOrder() {
            var count='';
            seajs.use("/script/shoppingcart/comfirmorder", function (comfirm) {
                $("#scenicbody").html(new EJS({url: "/views/shoppingcart/comfirmorder.ejs"}).render({
                    data: reservearray,
                    count:count
                }));
                COMFIRMORDER = comfirm;
            });
    }

    //我的
    function mineInfo() {
        $("#homepage").attr('src', "images/bar/homepage.png");
        $("#shopcart").attr('src', "images/bar/shopcart.png");
        $("#mine").attr('src', "images/bar/barcenterl.png");
        $("#shouye").removeClass("on-mine");
        $("#gouwuche").removeClass("on-mine");
        $("#wode").addClass("on-mine");

        $.get('/mine/mineproinfo?userid=' + user.id, function (res) {
            if (res.data.length > 0) {
                seajs.use("/script/mine/mineinfo", function (mineinfo) {
                    $("#scenicbody").html(new EJS({url: "/views/mine/mineinfo.ejs"}).render({
                        data: res.data
                        // activityWidth: activityWidth,
                        // activityHeight: activityHeight
                    }));
                    MINEINFO = mineinfo;
                });
            }
        })
    }

//我的公司信息
    function mineProfile() {
        $.get('/mine/mineprofile?userid='+user.id, function (res) {
                seajs.use("/script/mine/myprofile", function (mineprofile) {
                    $("#scenicbody").html(new EJS({url: "/views/mine/myprofile.ejs"}).render({
                        data: res.data
                    }));
                    MINEPRO = mineprofile;
                });
        })

    }

//我的订单
    function mineOrder() {

        $.get('/mine/mineorder?userid=' + user.id, function (res) {
            seajs.use("/script/mine/mineorder", function (mineorder) {
                $("#scenicbody").html(new EJS({url: "/views/mine/mineorder.ejs"}).render({
                    data: res.data.rows1,
                    data2:res.data.rows2
                }));
                MINEORDER = mineorder;
            });
        })
    }

    //我的消费
    function mineExpend() {
        $.get('/mine/mineexpend?userid='+user.id, function (res) {
            if (res.data) {
                var date = new Date();
                var year = date.getFullYear();
                var month = date.getMonth()+1;
                var d = new Date(year, month, 0);
                var num =d.getDate();
                seajs.use("/script/mine/mineexpend", function (mineexpend) {
                    $("#scenicbody").html(new EJS({url: "/views/mine/mineexpend.ejs"}).render({
                        allmoney: res.data.allmoney,
                        data:res.data.rows,
                        data_s:res.data.data_s,
                        num:num
                    }));

                    MINEEXPEND = mineexpend;
                });
            }
        })
    }

    //管理员待处理订单
    function adminPending() {
        $.get('/mine/adminpending', function (res) {
            seajs.use("/script/mine/adminpending", function (adminPending) {
                $("#scenicbody").html(new EJS({url: "/views/mine/adminpending.ejs"}).render({
                    data: res.data.rows1,
                    data2:res.data.rows2
                }));
                ADMINPEND = adminPending;
            });
        })
    }

    //待处理订单详情
    function pendingOrder(userid,id) {
        $.get('/mine/pendingorder?id=' + id+"&userid="+userid, function (res) {
            console.log(res);
            if(res.data.length>0){
                seajs.use("/script/mine/pendingorder", function (pendingOrder) {
                    $("#scenicbody").html(new EJS({url: "/views/mine/pendingorder.ejs"}).render({
                        data: res.data
                    }));
                    PENDORDER = pendingOrder;
                });
            }
        })
    }

    //我的意见
    function mineOpinion() {
        seajs.use("/script/mine/myopinion", function (mineopinion) {
            $("#scenicbody").html(new EJS({url: "/views/mine/myopinion.ejs"}).render({}));
            MINEOPINION = mineopinion;
        });
    }

    return {
        initApp: initApp,
        spotView: spotView,
        shoplist: shoplist,
        shoppingCart: shoppingCart,
        comfirmOrder: comfirmOrder,
        mineInfo: mineInfo,
        mineOrder: mineOrder,
        mineExpend: mineExpend,
        pendingOrder: pendingOrder,
        mineOpinion: mineOpinion,
        mineProfile: mineProfile,
        adminPending: adminPending
    }
});


